#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QCheckBox>
#include <QLabel>
#include <QSpacerItem>

#include <QMenu>
#include <QSystemTrayIcon>
#include <QCloseEvent>
#include <QMessageBox>

#include "mytimer.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *toolbar_spacer = new QWidget(this);
    toolbar_spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->toolBar->insertWidget(ui->actionInfo, toolbar_spacer);

    actionBypassDoNotDisturb = new QCheckBox(tr("Bypass do not disturb"), this);
    actionBypassDoNotDisturb->setToolTip(tr("Bypass do not disturb (B)"));
    actionBypassDoNotDisturb->setShortcut(tr("B"));
    actionBypassDoNotDisturb->setStyleSheet("color:blue");
    ui->toolBar->insertWidget(ui->actionInfo, actionBypassDoNotDisturb);

    ui->verticalLayout->addStretch(1);

    QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sp.setVerticalStretch(3);

    ptimer = new MyTimer(this);
    ptimer->setMinimumSize({300, 300});
    ptimer->setSizePolicy(sp);
    ui->verticalLayout->addWidget(ptimer);

    plabel = new QLabel(tr("I need to focus on..."), this);
    plabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    plabel->setAlignment(Qt::AlignHCenter);
    plabel->setStyleSheet("color:#BABFC6;font-size:24px;");
    ui->verticalLayout->addWidget(plabel);

    ui->verticalLayout->addStretch(1);

    connect(ptimer, &MyTimer::clicked, this, [&] {on_start_clicked();});
    connect(ptimer, &MyTimer::time_up, this, &MainWindow::on_timer_up);
    ptimer->set_lap(stage);
    ptimer->set_duration(stage_durations[stage]*60);

    connect(ui->actionResume, &QAction::triggered, this, &MainWindow::on_start_clicked);
    connect(ui->actionReset, &QAction::triggered, this, &MainWindow::on_reset_clicked);
    connect(ui->actionSkip, &QAction::triggered, this, &MainWindow::on_skip_clicked);
    connect(ui->actionInfo, &QAction::triggered, this, [&] {
        QMessageBox::information(this, tr("About"),
                                 tr("Francis(clone)\n---\nAuthor zhumo\nEmail zhumo25@outlook.com\nThanks using!"
                                    "\nAbout KDE Francis https://apps.kde.org/francis/"
                                    "\nAbout Qt https://www.qt.io/"));
    });


    // TRAYICON
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(ui->minimizeAction);
    trayIconMenu->addAction(ui->maximizeAction);
    trayIconMenu->addAction(ui->restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(ui->quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/icons/org.kde.francis.svg"));
    trayIcon->setContextMenu(trayIconMenu);

    connect(trayIcon, &QSystemTrayIcon::messageClicked, this, [&]{showNormal();});

    connect(ui->quitAction, &QAction::triggered, this, [] {QCoreApplication::quit();});
    connect(ui->restoreAction, &QAction::triggered, this, [&]{showNormal();});
    connect(ui->minimizeAction, &QAction::triggered, this, [&] {hide();});
    connect(ui->maximizeAction, &QAction::triggered, this, [&]{showMaximized();});

    trayIcon->show();
    // END
}

void MainWindow::on_start_clicked() {
    if (ui->actionResume->text() == "Start")  {
        ui->actionResume->setText(tr("Pause"));
        ui->actionReset->setVisible(true);
        ui->actionSkip->setVisible(true);

        next_stage(true);
    }
    else if (ui->actionResume->text() == "Resume") {
        ui->actionResume->setText(tr("Pause"));

        ptimer->start();
    }
    else {  // PAUSE
        ui->actionResume->setText(tr("Resume"));

        ptimer->pause();
    }
}

void MainWindow::on_reset_clicked() {
    ui->actionResume->setText(tr("Start"));
    ui->actionReset->setVisible(false);
    ui->actionSkip->setVisible(false);

    stage = 0;
    ptimer->set_lap(stage);
    ptimer->pause();
    ptimer->set_duration(stage_durations[stage]*60);
}

void MainWindow::on_skip_clicked() {
    next_stage();
}

void MainWindow::next_stage(bool run) {
    if (!run && !actionBypassDoNotDisturb->isChecked()) {
        // SEND SYSTEM NOTIFICATION
        trayIcon->showMessage(stage%2==0 ? tr("Interval Ended") : tr("Break Ended"),
                              stage%2==0 ? tr("Enjoy your break, drink some water.") : tr("Get back to work."));
    }
    if (stage == 8) {
        on_reset_clicked();
        return;
    }
    ui->actionResume->setText(tr("Resume"));
    if (!run) ptimer->pause();
    ptimer->set_lap(stage);
    plabel->setText(stage%2==0 ? tr("I need to focus on...") : tr("Taking a Break"));
    plabel->setStyleSheet(QString("color:%1;font-size:24px;").arg(stage%2==0 ? "#BABFC6" : "#232629"));
    ptimer->set_duration(stage_durations[stage]*60);
    if (run) ptimer->start();
    ++stage;
}

void MainWindow::on_timer_up() {
    next_stage();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (!event->spontaneous() || !isVisible())
        return;
    static bool is_first_close = true;
    if (trayIcon->isVisible()) {
        if (is_first_close) {
            trayIcon->showMessage(tr("Systray"),
                                  tr("The program will keep running in the "
                                     "system tray. To terminate the program, "
                                     "choose Quit in the context menu "
                                     "of the system tray entry."));
            is_first_close = false;
        }

        hide();
        event->ignore();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
